# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=collada-dom

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-svn"
_base_ver=2.4.1
pkgver=2.4.1.r889
pkgrel=2
pkgdesc="API that provides a C++ object representation of a COLLADA XML instance document (mingw-w64)"
arch=('any')
url="http://sourceforge.net/projects/collada-dom/"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "${MINGW_PACKAGE_PREFIX}-cmake" "subversion")
depends=(
    "${MINGW_PACKAGE_PREFIX}-bzip2"
    "${MINGW_PACKAGE_PREFIX}-zlib"
    "${MINGW_PACKAGE_PREFIX}-boost"
    "${MINGW_PACKAGE_PREFIX}-pcre"
    "${MINGW_PACKAGE_PREFIX}-libxml2")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
options=('staticlibs' 'strip' 'buildflags')
source=("${_realname}"::"svn+svn://svn.code.sf.net/p/collada-dom/code/trunk"
    collada-2.4.0-mingw-w64-fix.patch
    build-separate-libs.patch)
md5sums=('SKIP'
         '770dddcb6b498d3afc005fcebb129d98'
         'd8844a5233791acb033bbcfb8b10447a')

pkgver() {
  cd "$srcdir/${_realname}"
  local ver="$(svnversion)"
  printf "%s.r%s" "${_base_ver}" "${ver//[[:alpha:]]}"
}

prepare() {
  cd "${srcdir}/${_realname}"
  patch -p1 -i ${srcdir}/collada-2.4.0-mingw-w64-fix.patch
  patch -p1 -i ${srcdir}/build-separate-libs.patch
  #sed -i "s|#include <unzip.h>|#include <minizip/unzip.h>|g" dom/include/dae/daeZAEUncompressHandler.h
}

build() {
  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null

  pushd ${MINGW_PREFIX} > /dev/null
  export PREFIX_DEPS=`pwd -W`
  popd > /dev/null

  cp -r ${srcdir}/${_realname} ${srcdir}/build14
  cp -r ${srcdir}/${_realname} ${srcdir}/build15
  
  CFLAGS+=" -I ${MINGW_PREFIX}/include -I ${MINGW_PREFIX}/include/minizip"
  CPPFLAGS+=" -I ${MINGW_PREFIX}/include -I ${MINGW_PREFIX}/include/minizip"
  CXXFLAGS+=" -I ${MINGW_PREFIX}/include -I ${MINGW_PREFIX}/include/minizip"
  
  cd "${srcdir}/build14"
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${PREFIX_WIN} \
    -DOPT_COLLADA14:BOOL=ON \
    -DOPT_COLLADA15:BOOL=OFF \
    -DCMAKE_BUILD_TYPE=RELEASE \
    -DBOOST_ROOT=${PREFIX_WIN}
  make
  
  cd "${srcdir}/build15"
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${PREFIX_WIN} \
    -DOPT_COLLADA14:BOOL=OFF \
    -DOPT_COLLADA15:BOOL=ON \
    -DCMAKE_BUILD_TYPE=RELEASE \
    -DBOOST_ROOT=${PREFIX_WIN}
  make
}

package() {
  cd "${srcdir}/build14"
  make -j1 install
  cp dom/src/1.4/*.a ${pkgdir}${MINGW_PREFIX}/lib/
  mv ${pkgdir}${MINGW_PREFIX}/lib/cmake/collada_dom-2.4/collada_dom-config.cmake ${pkgdir}${MINGW_PREFIX}/lib/cmake/collada_dom-2.4/collada_dom14-config.cmake

  cd "${srcdir}/build15"
  make -j1 install
  cp dom/src/1.5/*.a ${pkgdir}${MINGW_PREFIX}/lib/
  mv ${pkgdir}${MINGW_PREFIX}/lib/cmake/collada_dom-2.4/collada_dom-config.cmake ${pkgdir}${MINGW_PREFIX}/lib/cmake/collada_dom-2.4/collada_dom15-config.cmake
  
  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  mv ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin/
  rm ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/collada-dom.pc
}
